//
//  HistoryViewController.m
//  51-Tour
//
//  Created by xiang ying on 14-3-20.
//  Copyright (c) 2014年 xiang ying. All rights reserved.
//

#import "HistoryViewController.h"
#import "NSDate+TPCategory.h"
#import "NSString+TPCategory.h"

@interface HistoryViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSArray     *_msgArray;
    
    UITableView *_hisList;
}

@end

@implementation HistoryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initNavigationItme:[[DataCenter instance] getStringForKey:@"History"]];
    _msgArray = [NSArray arrayWithContentsOfFile:MSGPATH];
    _hisList = [[UITableView alloc] initWithFrame:self.contentView.bounds];
    _hisList.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_hisList setBackgroundColor:[UIColor clearColor]];
    _hisList.dataSource = self;
    _hisList.delegate = self;
    [self.contentView addSubview:_hisList];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)popself{
    [self.navigationController popViewControllerAnimated:NO];
    [[UIManagement shareInstance] showRight];
}

#pragma mark - UITableViewDataSource & UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _msgArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        cell.backgroundColor = [UIColor clearColor];
        
        UILabel *_timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 280, 30)];
        _timeLabel.backgroundColor = [UIColor clearColor];
        _timeLabel.tag = 22;
        _timeLabel.font = [UIFont systemFontOfSize:14];
        _timeLabel.textColor = THEMECOLOR;
        [cell addSubview:_timeLabel];
        
        UILabel *_contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(_timeLabel.frame.origin.x, _timeLabel.frame.size.height, 290, 17)];
        _contentLabel.backgroundColor = [UIColor clearColor];
        _contentLabel.tag = 33;
        _contentLabel.textColor = [UIColor darkGrayColor];
        _contentLabel.font = [UIFont systemFontOfSize:13];
        [cell addSubview:_contentLabel];
        
        UIImageView *_line = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"list-line_14"]];
        _line.tag = 44;
        _line.frame = CGRectMake(5, cell.frame.size.height-1, 310, 1);
        [cell addSubview:_line];
    }
    
    UIImageView *_line = (UIImageView*)[cell viewWithTag:44];
    UILabel     *_timeLabel = (UILabel*)[cell viewWithTag:22];
    UILabel     *_contentLabel = (UILabel*)[cell viewWithTag:33];
    
    NSDictionary *dto = _msgArray[indexPath.row];
    
    _timeLabel.text = [NSDate stringFromDate:dto[@"date"] withFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    CGFloat height = [dto[@"msg"] heightwithfont:_contentLabel.font width:_contentLabel.frame.size.width];
    _contentLabel.numberOfLines = height/13+1;
    _contentLabel.frame = CGRectMake(_contentLabel.frame.origin.x, _contentLabel.frame.origin.y, _contentLabel.frame.size.width, height);
    _contentLabel.text = dto[@"msg"];
    _line.frame = CGRectMake(_line.frame.origin.x, height+30+5, _line.frame.size.width, 1);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dto = _msgArray[indexPath.row];

    CGFloat height = [dto[@"msg"] heightwithfont:[UIFont systemFontOfSize:13] width:290];
    
    return height+30+5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

@end
